package com.pdd.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.pdd.mall.dto.AddressParam;
import com.pdd.mall.entity.Address;
import com.pdd.mall.mapper.AddressMapper;
import com.pdd.mall.service.AddressService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 地址服务实现类
 */
@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressMapper addressMapper;

    @Override
    public List<Address> getAddressList(Long userId) {
        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.orderByDesc("is_default");
        return addressMapper.selectList(queryWrapper);
    }

    @Override
    public Address getAddress(Long id, Long userId) {
        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        queryWrapper.eq("user_id", userId);
        return addressMapper.selectOne(queryWrapper);
    }

    @Override
    @Transactional
    public Address addAddress(AddressParam param, Long userId) {
        Address address = new Address();
        BeanUtils.copyProperties(param, address);
        address.setUserId(userId);

        // 如果设置为默认地址，则将其他地址设置为非默认
        if (address.getIsDefault() != null && address.getIsDefault() == 1) {
            resetDefaultAddress(userId);
        }

        addressMapper.insert(address);
        return address;
    }

    @Override
    @Transactional
    public Address updateAddress(Long id, AddressParam param, Long userId) {
        Address address = getAddress(id, userId);
        if (address == null) {
            return null;
        }

        // 如果要设置为默认地址，则先将其他地址设置为非默认
        if (param.getIsDefault() != null && param.getIsDefault() == 1) {
            resetDefaultAddress(userId);
        }

        // 保存原始的关键字段，避免被BeanUtils.copyProperties覆盖
        Long originalId = address.getId();
        Long originalUserId = address.getUserId();
        
        BeanUtils.copyProperties(param, address);
        
        // 恢复关键字段
        address.setId(originalId);
        address.setUserId(originalUserId);
        
        addressMapper.updateById(address);
        return address;
    }

    @Override
    public boolean deleteAddress(Long id, Long userId) {
        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        queryWrapper.eq("user_id", userId);
        return addressMapper.delete(queryWrapper) > 0;
    }

    /**
     * 重置默认地址
     * 将用户的所有地址设置为非默认
     */
    private void resetDefaultAddress(Long userId) {
        UpdateWrapper<Address> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id", userId);
        updateWrapper.set("is_default", 0);
        addressMapper.update(null, updateWrapper);
    }
}
